package pre.bookcloud.java21.menu1;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Objects;

/**
 * Student data model for the attendance system
 * JSON format: {"name":"学生姓名","stu_no":"学生编号","sign_status":"签到状态","sign_date":"签到日期","gen_date":"表单生成日期"}
 */
public class Student {
    
    private String name;
    private String stuNo;
    private String signStatus;
    private LocalDate signDate;
    private LocalDateTime genDate;
    
    // Default constructor
    public Student() {
        this.genDate = LocalDateTime.now();
        this.signStatus = "未签到";
    }
    
    // Constructor with name and student number
    public Student(String name, String stuNo) {
        this();
        this.name = name;
        this.stuNo = stuNo;
    }
    
    // Getters and setters
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public String getStuNo() {
        return stuNo;
    }
    
    public void setStuNo(String stuNo) {
        this.stuNo = stuNo;
    }
    
    public String getSignStatus() {
        return signStatus;
    }
    
    public void setSignStatus(String signStatus) {
        this.signStatus = signStatus;
        if ("已签到".equals(signStatus)) {
            this.signDate = LocalDate.now();
        }
    }
    
    public LocalDate getSignDate() {
        return signDate;
    }
    
    public void setSignDate(LocalDate signDate) {
        this.signDate = signDate;
    }
    
    public LocalDateTime getGenDate() {
        return genDate;
    }
    
    public void setGenDate(LocalDateTime genDate) {
        this.genDate = genDate;
    }
    
    // Check if student is signed in
    public boolean isSignedIn() {
        return "已签到".equals(signStatus);
    }
    
    // Toggle sign status
    public void toggleSignStatus() {
        if (isSignedIn()) {
            setSignStatus("未签到");
            this.signDate = null;
        } else {
            setSignStatus("已签到");
        }
    }
    
    // Convert to JSON string
    public String toJson() {
        StringBuilder json = new StringBuilder();
        json.append("{");
        json.append("\"name\":\"").append(name != null ? name : "").append("\",");
        json.append("\"stu_no\":\"").append(stuNo != null ? stuNo : "").append("\",");
        json.append("\"sign_status\":\"").append(signStatus != null ? signStatus : "").append("\",");
        json.append("\"sign_date\":\"").append(signDate != null ? signDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) : "").append("\",");
        json.append("\"gen_date\":\"").append(genDate != null ? genDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) : "").append("\"");
        json.append("}");
        return json.toString();
    }
    
    // Create Student from JSON-like data (simplified parsing)
    public static Student fromJsonData(String name, String stuNo, String signStatus, String signDate, String genDate) {
        Student student = new Student(name, stuNo);
        student.setSignStatus(signStatus);
        
        if (signDate != null && !signDate.isEmpty()) {
            try {
                student.setSignDate(LocalDate.parse(signDate, DateTimeFormatter.ofPattern("yyyy-MM-dd")));
            } catch (Exception e) {
                // Ignore parse errors
            }
        }
        
        if (genDate != null && !genDate.isEmpty()) {
            try {
                student.setGenDate(LocalDateTime.parse(genDate, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            } catch (Exception e) {
                // Ignore parse errors
            }
        }
        
        return student;
    }
    
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return Objects.equals(stuNo, student.stuNo);
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(stuNo);
    }
    
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", stuNo='" + stuNo + '\'' +
                ", signStatus='" + signStatus + '\'' +
                ", signDate=" + signDate +
                ", genDate=" + genDate +
                '}';
    }
}